﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using System.Data.SqlClient;
using System.Data;
using BusinessLayer.WorldArtPrice.Models;
using BusinessLayer.WorldArtPrice.Entities;

namespace BusinessLayer.WorldArtPrice
{
    public class CountryBL
    {
        private readonly DbHelper _db;

        public CountryBL()
        {
            _db = new DbHelper(new DbConfig().DbWorldArtPriceConnectionString);         
        }

        public void Insert(Countrys country)
        {
            var oCommand = new SqlCommand("sp_Country_Insert") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Country", country.Country));
            _db.ExecuteNonQuery(oCommand);
        }

        public void Update(Countrys country)
        {
            var oCommand = new SqlCommand("sp_Country_Update") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", country.Id));
            oCommand.Parameters.Add(new SqlParameter("@Country", country.Country));
            _db.ExecuteNonQuery(oCommand);
        }

        public void Delete(int id)
        {
            var oCommand = new SqlCommand("sp_Country_Delete") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", id));
            _db.ExecuteNonQuery(oCommand);
        }

        public List<Countrys> Search(string keyWord, int pageIndex, int pageSize, out int totalRecord)
        {
            var oCommand = new SqlCommand("sp_Country_Select_By_KeyWord") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@KeyWord", keyWord));
            oCommand.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            oCommand.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            var output = new SqlParameter("@TotalRecord", SqlDbType.Int) { Direction = ParameterDirection.Output };
            oCommand.Parameters.Add(output);
            var list = _db.GetList<Countrys>(oCommand);
            totalRecord = (int)output.Value;
            return list;
        }

        public List<Countrys> GetAll()
        {
            var oCommand = new SqlCommand("sp_Country_Select_All") { CommandType = CommandType.StoredProcedure };
            var listCountry = _db.GetList<Countrys>(oCommand);
            return listCountry; 
        }

        public Countrys GetById(int id)
        {
            var oCommand = new SqlCommand("sp_Country_Select_By_Id") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Id", id));
            var listCountry = _db.GetList<Countrys>(oCommand);
            return listCountry.FirstOrDefault();
        }

        public Countrys GetByName(string country)
        {
            var oCommand = new SqlCommand("sp_Country_Select_By_Country") { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@Country", country));
            var listCountry = _db.GetList<Countrys>(oCommand);
            return listCountry.FirstOrDefault();
        }
    }
}
